package cn.com.SocketServers;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServers {
	 public static void main(String[] args) {
	        try {
	            /** 创建ServerSocket*/
	            // 创建一个ServerSocket在端口6666监听客户请求
	            ServerSocket serverSocket =new ServerSocket(6666);
	            while (true) {
	                // 侦听并接受到此Socket的连接,请求到来则产生一个Socket对象，并继续执行
	                Socket socket = serverSocket.accept();
	  
	                /** 获取客户端传来的信息 */
	                // 由Socket对象得到输入流，并构造相应的BufferedReader对象
	                BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
	                // 获取从客户端读入的字符串
	                String result = bufferedReader.readLine();
	                System.out.println("来自客户端的信息 : " + result);
	  
	                /** 发送服务端准备传输的 */
	                // 由Socket对象得到输出流，并构造PrintWriter对象
	                BufferedWriter stdout = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8")); //设置输出接口按中文编码
	                stdout.write("客户端你好, 我是服务器!");
	                stdout.flush();
	  
	                /** 关闭Socket*/
	                stdout.close();
	                bufferedReader.close();
	                socket.close();
	            }
	        }catch (Exception e) {
	            System.out.println("Exception:" + e);
	        }finally{
//	          serverSocket.close();
	        }
	    }

}
